package com.qdsg.modulesihealsdk.detectStrategy.strategy.hwbmiStrategy;

import android.content.Context;

import com.example.moduleutils.LogUtil;
import com.google.gson.Gson;
import com.qdsg.modulesihealsdk.detectStrategy.control.StrategyCallback;
import com.qdsg.modulesihealsdk.entity.HwBmiResultEntity;
import com.qdsg.modulesihealsdk.utils.SihealSdkConstant;
import com.qdsg.modulesihealsdk.detectStrategy.control.DetectBusiness;
import com.siheal.device.reader.measure.HeightWeightReader;
import com.siheal.device.reader.singlechip.DataChannelReader;
import com.siheal.entitys.device.DeviceOp;
import com.siheal.entitys.measure.HeightWeightEntity;
import com.siheal.exception.BasicException;

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2022/11/21 11:53
 */
public class HwBmiStrategy implements DetectBusiness {
    private DataChannelReader dataChannelReader;
    private HeightWeightReader heightWeightReader;
    private Context context;
    private HwBmiCallback hwBmiCallback;
    private int progress = 0;

    /**
     *
     * haveGetReceive：是否已出值标志，防止出值后还去调用process方法
     */
    private Boolean haveGetReceive = false;
    @Override
    public void dealDetect(Context context, StrategyCallback strategyCallback) {
        this.context = context;
        haveGetReceive = false;
        progress = 0;
        if (strategyCallback instanceof HwBmiCallback){
            hwBmiCallback = (HwBmiCallback)strategyCallback;
            hwBmiCallback.detectProgress(0);
            detectHeightWeight();
        }
    }

    @Override
    public void stopThread() {
        try {

            if (heightWeightReader != null){
                heightWeightReader.stopRead();
                heightWeightReader = null;
            }
        }catch (Exception e){

        }
        if (hwBmiCallback != null){
            hwBmiCallback = null;
        }
        haveGetReceive = false;
    }

    private void detectHeightWeight(){
        dataChannelReader = (new DataChannelReader(SihealSdkConstant.channelID_HWBMI, context){
            // 数据通道切换成功，开始调用身高体重读值方法
            @Override
            public void resultReceived(DeviceOp result) {

                heightWeightReader = (new HeightWeightReader(context) {
                    //身高体重数据获取成功，开始做后续数据处理，当前操作将结果数据回显至H5页面
                    //获取一次结果(最终结果)，在此方法中接收检测结果
                    @Override
                    public void resultReceived(HeightWeightEntity result) {

                        //身高偏差值设定，measureHeightDeviate，measureWeightDeviate来源于配置文件设定
                        String data = new Gson().toJson(result);
                        LogUtil.logAndWrite("__hwBmi-resultReceived",data);

//                                    {"assess":"MILD_HIGH","height":167.7,"weight":73.8,"c":"3e9344ed146b4914ab8c55ee81ad8f34","d":0,"e":0,"gen_plan":true,"a":1668754735396,"b":0}
                        //得到数据
                        progress = 0;
                        if (hwBmiCallback != null && !haveGetReceive){
                            hwBmiCallback.detectProgress(100);
                            haveGetReceive = true;
                            hwBmiCallback.resultReceived(new HwBmiResultEntity(result));
                        }
                    }

                    //无需处理
                    @Override
                    public void process(HeightWeightEntity middleResult) {
                        String data = new Gson().toJson(middleResult);
                        LogUtil.logAndWrite("__hwBmi-process",data);
                        if (middleResult != null){
                            if (middleResult.getHeight() == 0f && middleResult.getWeight() == 0f){
                                detectHeightWeight();
                                return;
                            }
                        }
                        if (!haveGetReceive && hwBmiCallback != null){
                            if (progress < 90){
                                progress = progress + 10;
                                hwBmiCallback.detectProgress(progress);
                            }
                            hwBmiCallback.process(new HwBmiResultEntity(middleResult));
                        }

                    }
                    //调用过程中出现的异常
                    @Override
                    public void occurError(BasicException e) {

                                   /*
                                    int MEASURE_OUT_RANGE_EXCEPTION = 1001;//测量失败建议重测 weight <= 10 || weight >= 200 height <= 0 || height >= 200
                                    int MEASURE_TIME_OUT = 1005;//测量超时
                                   */
                        String data = new Gson().toJson(e);
                        LogUtil.logAndWrite("__hwBmi-occurError",data);
                        //得到数据
                        if (hwBmiCallback != null){
                            hwBmiCallback.occurError(e);
                        }
                    }
                });
                if (heightWeightReader != null){
                    heightWeightReader.start();
                }
            }
            //数据通道切换process方法，实现了父类抽象方法，在此无实义
            @Override
            public void process(DeviceOp middleResult) {

            }
            //数据通道切换异常处理方法
            @Override
            public void occurError(BasicException exception) {

                          /*  int ERROR_IO = 1;     //打开设备" + device.getDeviceID() + "数据通道异常
                            int ERROR_WRITE_DATA = 2;//设备" + device.getDeviceID() + "写入数据发生异常
                            int ERROR_USB_NOT_FOUND = 2001;//没有找到指定地址下(" + device.getPath() + ")设备
                            int ERROR_USB_NOT_SUPPORT = 2002;//本系统不支持USB模块
                            int ERROR_USB_UNKONW_TYPE = 2003;//未知的USB设备类型
                            int ERROR_USB_REQUEST_PERMISSION = 2004;//请求usb权限失败
                             int MEASURE_PROBE_CONFIG_ERROR = 1008;//配置文件配置错误
                            int ERROR_USB_DEVICE_OPEN = 2005;//打开usb设备错误*/
                if (hwBmiCallback != null){
                    hwBmiCallback.occurError(exception);
                }
            }
        });
        if (dataChannelReader != null){
            dataChannelReader.start();
        }
    }
}
